﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.POConfig.System
{
    public class UserConfig : POBaseConfig<UserPO>
    {
        public override void InitializeData(EntityTypeBuilder<UserPO> builder)
        {
            builder.HasData(new UserPO()
            {
                Id = SnowflakeIdHelper.Instance.NextId(),
                Name = "Admin",
                Account = "LWMS",
                Password = EncryptHelper.Encrypt("Nti56.com"),
                LoginIp = string.Empty,
                LoginCount = 0,
                LoginErrorCount = 0,
                IsLocked = false,
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });
        }

        public override void MapColumn(EntityTypeBuilder<UserPO> builder, ref int index)
        {
            builder.HasComment("用户表");
            builder.Property(entity => entity.Name).ConfigColumn("NAME", "用户姓名", 100, true).HasColumnOrder(index++);
            builder.Property(entity => entity.Account).ConfigColumn("ACCOUNT", "用户账号", 100, true).HasColumnOrder(index++);
            builder.Property(entity => entity.Password).ConfigColumn("PASSWORD", "用户密码", 200, true).HasColumnOrder(index++);
            builder.Property(entity => entity.LoginIp).ConfigColumn("LOGIN_IP", "登录IP", 50).HasColumnOrder(index++);
            builder.Property(entity => entity.LoginCount).ConfigColumn("LOGIN_COUNT", "登录次数").HasColumnOrder(index++);
            builder.Property(entity => entity.LoginErrorCount).ConfigColumn("LOGIN_ERROR_COUNT", "密码错误次数").HasColumnOrder(index++);
            builder.Property(entity => entity.IsLocked).ConfigColumn("IS_LOCKED", "是否被锁定").HasColumnOrder(index++);
        }

        public override void MapIndex(EntityTypeBuilder<UserPO> builder)
        {
            builder.HasIndex(entity => entity.Account).HasDatabaseName($"UN_{TableName}_1").IsUnique();
        }
    }
}
